package com.updesignsz.admin.mapper;

import com.updesignsz.admin.vo.form.ModuleForm;
import com.updesignsz.commom.entity.Module;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * TODO
 * Created User: lx
 * Date: 2020/4/28 15:05
 */
@Repository
@Mapper
public interface ModuleMapper {

    @Select("select identifier from t_module where id in (${moduleIds})")
    List<String> findByModuleIdList(@Param("moduleIds") String moduleIds);

    @Select("select * from t_module where id = #{moduleId}")
    Module findModuleByFunctionId(@Param("moduleId") Integer moduleId);

    @Select("select * from t_module order by created_date desc")
    List<Module> listAll();


    @Select("select * from t_module where id = #{id}")
    Module getById(@Param("id") Integer id);

    @Insert({
            "insert into t_module(name, identifier, remark, created_date, updated_date, is_removed) ",
            "values (#{name},#{identifier},#{remark},NOW(),NOW(),0)"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id")
    void saveModule(ModuleForm module);

    @Update({"<script>",
            "update t_module <set>",
            "<if test=\"identifier != null\" >identifier = #{identifier},</if>",
            "<if test=\"name != null\" >name = #{name},</if>",
            "<if test=\"remark != null\" >remark = #{remark},</if>",
            "updated_date = NOW()",
            "</set> where id = #{id}",
            "</script>"
    })
    void updateModule(ModuleForm module);

    @Delete("delete from t_module where id = #{id}")
    void deleteModule(@Param("id") Integer id);


}
